﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Model.Model;
using CSP.Model.Entities;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using CSP.Model.DataAccess;


namespace CSP.Model.Presenter
{
    public class MyPeepsUpdatesPresenter
    {
        private IMyPeepsUpdatesPresenter view = null;
        private ModelManager model = new ModelManager();

        public MyPeepsUpdatesPresenter(IMyPeepsUpdatesPresenter iView)
        {
            view = iView;
        }

        public void Load()
        {
            List<Update> updates = new List<Update>();
            try
            {
                List<UserChange> changes = model.GetColleagueUpdates();

                UserChangeCollection col = new UserChangeCollection();
                foreach (UserChange change in changes)
                {
                    col.Add(change);
                }
                col.SortColumn = "changeDate";
                col.SortingOrder = GenericComparer.SortOrder.Descending;
                col.Sort();





                foreach (UserChange uc in col)
                {
                    if (uc.changeDate != DateTime.MinValue)
                        updates.Add(FormatUpdate(uc));
                }


            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                view.BindMyPeepsUpdates(updates);
            }
            
        }


        private Update FormatUpdate(UserChange uc)
        {
            Update update = new Update();
            
            try
            {
                update.ColleagueName = uc.UserNameChange;
                update.EventTime = uc.changeDate.ToShortDateString();
                update.ColleagueEmail = uc.colleagueEmail;


                if (!string.IsNullOrEmpty(uc.changeDate.ToString()))
                {
                    update.EventTime = SPDataAccess.FormatPostedTime(uc.changeDate);
                    //string[] timeSpan = DateTime.Now.Subtract(uc.changeDate).ToString().Split(':');

                    //string eventTime = timeSpan[0]!="0"?string.Format("{0} hr(s) ",timeSpan[0]):"";
                    //eventTime += timeSpan[1]!="0"?string.Format("{0} min(s)",timeSpan[1]):"";
                    //update.EventTime = eventTime + " ago.";
                }
                //if (uc.changeDate.ToShortDateString() == DateTime.Today.ToShortDateString())
                //{
                //    string[] timeSpan = DateTime.Now.Subtract(uc.changeDate).ToString().Split(':');

                //    string eventTime = timeSpan[0] != "0" ? string.Format("{0} hr(s) ", timeSpan[0]) : "";
                //    eventTime += timeSpan[1] != "0" ? string.Format("{0} min(s)", timeSpan[1]) : "";
                //    update.EventTime = eventTime + " ago.";
                //}

                SPWebApplication webApp = SPContext.Current.Site.WebApplication;
                SPSiteCollection siteColl = webApp.Sites;

                string webAppUrl = siteColl[0].Url;

                update.ColleaguePictureUrl = uc.colleaguePictureUrl;
                update.ColleagueAccount = webAppUrl + "/Person.aspx?accountname=" + uc.colleagueNameChange;

                switch (uc.UserChangeType)
                {
                    //case UserChange.UserChangeTypes.AnniversaryChange:
                    //    update.UpdateEvent = string.Format("{0}'s anniversary has changed to: {1}", uc.UserNameChange, uc.anniversaryChange);
                    //    update.UpdateIcon = "anniversary";
                    //    break;
                    case UserChange.UserChangeTypes.Tweet:
                        {
                            update.ColleagueName = uc.UserNameChange;
                            update.UpdateEvent = uc.blogTitleChange;
                            update.EventTime = SPDataAccess.FormatPostedTime(uc.changeDate);
                            break;
                        }
                    case UserChange.UserChangeTypes.BlogChange:
                        update.UpdateEvent = string.Format("Posted a new blog: <b><a href='{0}' target='_new'>'{1}'</a></b>", uc.blogURLChange, uc.blogTitleChange);
                        update.ColleagueName = uc.UserNameChange;
                        update.EventTime = SPDataAccess.FormatPostedTime(uc.changeDate);
                        break;
                    case UserChange.UserChangeTypes.DocumentChange:
                        switch (uc.DocumentChangeType)
                        {
                            case UserChange.DocumentChangeTypes.Added:
                                update.UpdateEvent = string.Format("Posted a new document: {0}", uc.documentNameChange);
                                break;
                            case UserChange.DocumentChangeTypes.Deleted:
                                update.UpdateEvent = string.Format("Deleted document: {0}", uc.documentNameChange);
                                break;
                            case UserChange.DocumentChangeTypes.Updated:
                                update.UpdateEvent = string.Format("Updated document: {0}", uc.documentNameChange);
                                break;
                        }
                        break;

                    //case UserChange.UserChangeTypes.ColleagueChange:
                    //    if (uc.ColleagueChangeType == UserChange.ColleagueChangeTypes.Add)
                    //    {
                    //        update.UpdateEvent = string.Format("{0} and <b>{1}</b> are now friends", uc.UserNameChange, uc.colleagueNameChange);
                    //    }
                    //    else
                    //    {
                    //        update.UpdateEvent = string.Format("{0} removed {1} from Colleagues list", uc.UserNameChange, uc.colleagueNameChange);
                    //    }
                    //    update.UpdateIcon = "colleague";
                    //    break;
                    //case UserChange.UserChangeTypes.SiteMembershipChange:
                    //    update.UpdateEvent = string.Format("{0} has joined the membership: <b>{1}</b>", uc.UserNameChange, uc.membershipChange);
                    //    update.UpdateIcon = "membership";
                    //    break;
                    //case UserChange.UserChangeTypes.StatusChange:
                    //    update.UpdateEvent = string.Format("Status update: {0}", uc.statusChange);
                    //    update.UpdateIcon = "status";
                    //    break;


                }
            }
            catch
            {
                throw;
            }
            return update;
        }
    }
}
